package process;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;

import android.os.Handler;

public class SendFileUDP {
//	private String m_host = "10.0.2.2";
	private int i_port = 8282;
	
//	private String m_Path = "/sdcard";
	//private String m_filename = "b.ppt";
	private int size = 1400;
	private String m_Start = "SendFileName";
	private String m_Content = "SendContenFile";
	private String m_End = "Kết thúc";
	private InputStream is;
	private FileInputStream fis;
	private BufferedInputStream bis;
	
	
	public String SendUsingUDP(String m_Path ,String m_filename,String ArrayOfIP)
	//public String SendUsingUDP(String m_Path ,String m_filename,String[] ArrayOfIP)
	{
		String thongbao ="";
		SendAllFile send = new SendAllFile();
		int port = 8080;
		// Khue Has change here 
		//for(int i=0;i<ArrayOfIP.length;i++)
		{
			String m_host=ArrayOfIP;
			//String m_host=ArrayOfIP[i];
		int kq = send.SendInfoStartSendUDP(m_host, i_port, m_Start);
		
	//	int kq3 = send.sendInfoEndSend(m_host, i_port, m_End);
		if(kq==1)
		{
			
			//int ip = Integer.parseInt(m_host);
			//ServerSocket serverSocket;
			/*String line = "";
			try {
			 DatagramSocket serverSocket = new DatagramSocket(i_port);
				byte[] receiveData = new byte[1024];
				DatagramPacket receivePacket = new DatagramPacket(receiveData,receiveData.length);
				serverSocket.receive(receivePacket);

				//serverSocket = new ServerSocket(i_port);
				//Socket client = serverSocket.;
				line = new String(receivePacket.getData());
				// BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
		       // line=in.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 if(line.equals("OK"))
			 {
		*/
			int ports = 8181;
				int kq1 = send.sendfilename(m_host, ports, m_filename);
				/*try {
					//Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					tte.printStackTrace();
				}*/
				if(kq1!=0)
				{
					int portss = 8383;
				//	int kq2 = send.SendfilenameUDP(m_host, i_port, m_Content);
					int kq3 = send.SenContentFile(m_Path, m_filename, m_host, portss);
					thongbao = "successful"+kq3;
					
				}
				else
				{
					thongbao = "unsuccessful";
				}
			 }
		}
		//}
		
		
		return thongbao;
		
		
	}
	public class udp_SendToAllThread implements Runnable {

		final Handler myHandler = new Handler();
		@Override
		public void run() {
			// TODO Auto-generated method stub
			(new Thread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					myHandler.post(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							
						}
					});
				}
			})).start();
		}
		
	}
}

